package com.pachong.android.frameworkbase.utils;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.text.TextUtils;

import java.io.File;

/**
 * 处理app相关的工具类
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-06-20
 * @Version: 1.0.0
 */
public class AppUtil {

	/**
	 * 判断是否安装了packageName的apk
	 *
	 * @param context
	 * @param packageName
     * @return
     */
	public static boolean hasPackage(Context context, String packageName) {
		if (!TextUtils.isEmpty(packageName)) {
			PackageInfo packageInfo = null;

			try {
				packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
			} catch (NameNotFoundException e) {
				packageInfo = null;
			}

			return packageInfo != null;
		}
		return false;
	}


	/**
	 * 安装app
	 * @param context
	 * @param uri
     */
	public static void installApp(Context context, Uri uri) {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		context.startActivity(intent);
	}


	public static void installAppByAlarm(Context context, File apkFile) {
		installAppByAlarm(context, Uri.fromFile(apkFile));
	}


	/**
	 * 通过注册alarm，让系统发出安装请求来安装apk。 此方法不会被360监听到。
	 *
	 * @param context
	 * @param uri
	 */
	private static void installAppByAlarm(Context context, Uri uri) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0,
				PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
	}


	/**
	 * 启动app
	 * @param context
	 * @param packName
     * @return
     */
	public static boolean startApp(Context context, String packName) {
		if (!TextUtils.isEmpty(packName)) {
			Intent intent = context.getPackageManager().getLaunchIntentForPackage(packName);
			if (context != null) {
				context.startActivity(intent);
				return true;
			}
		}
		return false;
	}


	
	public static PackageInfo getApkPackageInfo(Context context, String absApkPath) {
		PackageManager pm = context.getPackageManager();
		PackageInfo info = pm.getPackageArchiveInfo(absApkPath, PackageManager.GET_ACTIVITIES);
		return info;
	}


	public static PackageInfo getPackageInfo(Context context) {
		PackageInfo pi = null;
		try {
			PackageManager pm = context.getPackageManager();
			pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
			return pi;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pi;
	}


	/**
	 * 判断是否是apk文件
	 *
	 * @param conetxt
	 * @param file
	 * @return
	 */
	public static boolean isApkFile(Context conetxt, File file) {
		PackageInfo packInfo = AppUtil.getApkPackageInfo(conetxt, file.getAbsolutePath());
		return packInfo != null;
	}


	/**
	 * 解析apk文件， 获取app name
	 * @param context
	 * @param absFilename
     * @return
     */
	public static String getAppName(Context context, String absFilename) {
		String appName = "";
		PackageInfo info = AppUtil.getApkPackageInfo(context, absFilename);
		if (info != null) {
			ApplicationInfo appInfo = info.applicationInfo;
			PackageManager pm = context.getPackageManager();
			appInfo.sourceDir = absFilename;
			appInfo.publicSourceDir = absFilename;
			appName = pm.getApplicationLabel(appInfo).toString();
		}
		return appName;
	}


	/**
	 * no test
	 * @param context
	 * @return
	 */
	public static String getApplicationName(Context context) {
		PackageManager packageManager = null;
		ApplicationInfo applicationInfo = null;
		try {
			packageManager = context.getApplicationContext().getPackageManager();
			applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
		} catch (NameNotFoundException e) {
			applicationInfo = null;
		}
		String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
		return applicationName;
	}


	/**
	 * 版本名
	 *
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context) {
		return getPackageInfo(context).versionName;
	}

	/**
	 * 版本号
	 *
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		return getPackageInfo(context).versionCode;
	}

}
